﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PartsBalanceAdd.aspx.cs" Inherits="IHISCETeim.Web.MachineSys.PartsModel.PartsBalanceAdd" %>
<%--添加冲帐--%>
<script type="text/javascript">
    function validateCallbackPartsBalanceAdd(form, callback) {
        var $form = $(form);
        $("#PartsBalanceAddbtnSave").find("button").attr("disabled", "disabled").html("保存中......");
        setTimeout(function () { $("#PartsBalanceAddbtnSave").find("button").removeAttr("disabled").html(" 保 存 "); }, 10000);
      //  return false;
        return validateCallback(form, callback);
    }
</script>
<div class="pageContent">
	<form  method="post" action="PartsModel/PartsBalanceAdd.aspx?ajaxmethod=add&CId=<%=CId %>" class="pageForm required-validate" onsubmit="return validateCallbackPartsBalanceAdd(this, navTabAjaxDone);" >
		<div class="pageFormContent" layoutH="56">
            <h2 style="clear:both; padding:5px; border-bottom:1px solid #B8D0D6; margin-bottom:5px; ">
                收款基本信息
            </h2>
                <p>
				        <label>经 销 商：</label>
                         <input type="text" name="dwz.orgLookup.CardName" class="textInput" readonly alt="" readonly="true" value="<%=objShow.CardName %>" />                         
			        </p>
                    <p>
				        <label>经销商代码：</label>
				        <input name="dwz.orgLookup.CardCode" class="" type="text" readonly  value="<%=objShow.CardCode %>" alt=""/>
			        </p>
             <div class="divider"></div>
               <p>
				    <label>收款日期：</label>
				    <input name="ShoukuanRiQi" class="date " readonly type="text" value="<%=objShow.ShoukuanRiQi.ToString("yyyy-MM-dd") %>" alt=""/>
			   </p>
            
              <p>
				    <label>金     额：</label>
				    <input  name="JinE" class=" " type="text" readonly value="<%=objShow.JinE %>" size="" alt=""/>
                    <span class="unit">元</span>
			   </p>
              <p>
                    <label>剩余金额:</label>
                    <input id="PartsBalanceAdd_ShengYuJinE"  name="ShengYuJinE"  value="<%=objShow.ShengYuJinE %>" readonly/>                    
                </p>
              <div class="divider"></div>
               <p>
                    <label>款项类型:</label>
                   <input  name="JinE" class=" " type="text" readonly value="<%=objShow.KuanXiangLeiXing %>" size="" alt=""/>
                                     
                </p>
                <div class="divider"></div>
                <label>备注：</label>
				<textarea name="Remark" cols="80" rows="4" readonly ><%=objShow.Remark %></textarea>
                <div style="clear:both;margin-bottom: 5px;"></div>
            <div class="divider"></div>
            <h2 style="clear:both; padding:5px; border-bottom:1px solid #B8D0D6; margin-bottom:5px; margin-top:15px; line-height:26px;">
                本次冲帐
                &nbsp;&nbsp;
                <a style=" float:right;" class="button" href="javascript:openPartsDebtlLookUpList();"><span>添加需冲帐记录</span></a>
            </h2>
            <div  style=" clear:both;">
                <table id="PartsBalanceAdd_AddBalanceList" class="list" width="100%" layouth="90" >
                <thead>
                    <tr  >                
                        <th width="80" >经销商</th>
                        <th>销售日期</th>                
                        <th width="80">销售金额</th>                        
                        <th>已付</th> 
                        <th>未付</th> 
                        <th width="120">到期日</th> 
                        <th>本次冲帐</th>
                        <th>记账日期</th> 
                        
                        <th>操作</th>             
                    </tr>
                 </thead>
                 <tbody>
                    <%--<tr >
				        <td><input type="text" readonly value="[机号]" /></td>
				        <td><input type="text" readonly value="[款项名称]" /></td>
				        <td><input type="text" readonly value="[金额]" /></td>
                        <td><input type="text" readonly value="[到期日]" /></td>
                        <td><input type="text" readonly value="[已付]" /></td>
                        <td>
                            <input type="hidden" name="FTradeAgeId" value="[应收帐款明细]" />
                            <input type="text" name="BalanceMoney" value="[本次冲帐]" />
                        </td>
                        <td>
                            <input type="hidden" name="FnncLsId" value="[FnncLsId]" /><br/>
                            <span class="FXLPayShow"></span>
                        </td>
			            <td>
					        <a href="javascript:onPartsDebtSelectAddDelete(this)" title="删除">删除</a>
				        </td>
			        </tr>--%>
                 </tbody>
                 <tfoot>
                    <tr>
                        <td colspan="6" style="text-align:right;">
                        <span style="color:Red; line-height:25px;" id="PartsBalanceAdd_AddBalanceList_AllBalance_Hit"></span>
                        本次冲帐总计:
                        </td>
                        <td colspan="2" >
                            <input type="text" id="PartsBalanceAdd_AddBalanceList_AllBalance" size="10" readonly value="0" />                            
                        </td>
                        
                    </tr>
                 </tfoot>
                 </table>           
            </div>
            <script>
                $(document).ready(function () {
                    if ('<%=objShow.State %>' != '未冲完') {
                        alertMsg.error('对不起,此收款记录已冲完/已取消,无法进行冲帐操作！');
                        navTab.closeCurrentTab();
                        return;
                    }
                    $("#PartsBalanceAddbtnSave").hide();
                    openPartsDebtlLookUpList();

                })
                function openPartsDebtlLookUpList() {
                    var rel = "PartsDebtlLookUpList";
                    var url = "PartsModel/PartsDebtlLookUpList.aspx?CardCode=" + encodeURI("<%=objShow.CardCode %>") ;
                    var title = "选择可冲帐的记录-【<%=objShow.CardName %>】--->可冲帐金额为:〖<%=objShow.ShengYuJinE %>〗";
                    var options = { width: 950, height: 450, max: false, mask: true, maxable: false, minable: false, fresh: true, resizable: false, drawable: true };
                    top.$.pdialog.open(url, rel, title, options);
                }

                function onPartsDebtSelectAdd(objId,rowDate) {
                    //处理原记录所在行格式
                   
                    var $totalmoney = $("#PartsDebtLookUpListShowTotalMoney").val();
                    var $ShengYuJinE = $("#PartsBalanceAdd_ShengYuJinE").val();
                   // alert(parseFloat($ShengYuJinE))
                   // alert( (parseFloat($totalmoney) + parseFloat($(objId).val()) ) )
                    if (parseFloat($ShengYuJinE) < (parseFloat($totalmoney) + parseFloat($(objId).val())) ) {
                        alertMsg.warn("对不起,所选冲帐金额已大于本次最大能冲帐的金额.");
                        return;
                    }
                    $("#PartsDebtLookUpListShowTotalMoney").val(parseFloat($totalmoney) + parseFloat($(objId).val()));
                    $("#PartsDebtLookUpListShowTotalMoneyCanUse").html(parseFloat($("#PartsBalanceAdd_ShengYuJinE").val()) -parseFloat( $("#PartsDebtLookUpListShowTotalMoney").val()));
                    var $jtr = $(objId).closest("tr");
                    $jtr.css("color", "red").find("input").attr("readonly", true);
                    $jtr.find("a.btnSelect").hide();
                    $jtr.find("a.btnSelectFXLPay").hide();
                    $jtr.find("a.btnDel").show();  
                    //
                    var $BalanceMoney = $(objId).val();
                 
                    var $AddBalanceListTbody = $("#PartsBalanceAdd_AddBalanceList").find("tbody");
                    var rowExist = $AddBalanceListTbody.find("#PartsBalanceAdd_AddBalanceList_Row" + rowDate.DebtId).attr("id");
                    if (rowExist != undefined) {
                        alertMsg.warn("对不起,已添加到要冲帐的记录中,请选择其它冲帐记录");
                        return;
                    }
                    var htmltemp = "<tr id='PartsBalanceAdd_AddBalanceList_Row" + rowDate.DebtId + "' >" +
				                    "<td><input type=\"text\" class='readonly'  size='10' readonly value=\"[经销商]\" /></td>" +
                                     "<td><input type=\"text\" class='readonly'  size='10' readonly value=\"[销售日期]\" /></td>" +
				                    "<td><input type=\"text\" class='readonly' size='10' readonly value=\"[销售金额]\" /></td>" +
				                    "<td><input type=\"text\" class='readonly' size='10' readonly value=\"[已付]\" /></td>" +
                                    "<td><input type=\"text\" class='readonly' size='10' readonly value=\"[未付]\" /></td>" +
                                    "<td><input type=\"text\" class='readonly' size='10' readonly value=\"[到期日]\" /></td>" +
                                    "<td>"+
                                    "    <input type=\"hidden\" name=\"DebtId\" value=\"[DebtId]\" />" +
                                    "    <input type=\"text\" size='10' class='inputFloat' name=\"BalanceMoney\" value=\"[本次冲帐]\" />" +
                                    "</td>" +
                                    "<td><input type=\"text\" size='10' class='date' name=\"CreateDate\" value=\"[记账日期]\" /></td>" +
                                    "<td>"+
					                "    <a href=\"#\" title=\"删除\">删除</a>"+
				                    "</td>"+
			                    "</tr>";
                    var addHtml = htmltemp.replace("[经销商]", rowDate.CardName).replace("[销售日期]", rowDate.SaleDate).replace("[销售金额]", rowDate.SalesAmount).replace("[已付]", rowDate.ReceivedAmount).replace("[未付]", rowDate.WeiShouAmount).replace("[到期日]", rowDate.DueDate).replace("[DebtId]", rowDate.DebtId).replace("[本次冲帐]", $BalanceMoney).replace("[记账日期]", rowDate.CreateDate);
                    $AddBalanceListTbody.append($(addHtml).find("a").click(function () {
                        onPartsDebtSelectAddDelete($(this));
                    }).end().find(":text[name='BalanceMoney']").change(function () {

                        var $maxmoney = parseFloat($BalanceMoney);
                        var $inputmoney = parseFloat($(this).val());
                        if ($inputmoney <= 0) {
                            alert("你所输入的冲帐金额小于0,请重输");
                            $(this).val("0");
                            return false;
                        }
                        if ($inputmoney > $maxmoney) {
                            alert("你所输入的冲帐金额大于可用金额[" + $maxmoney + "],请重输");
                            $(this).val($maxmoney)
                            return false;
                        }

                        onPartsDebtSelectAddChange($(this));
                    }).end()
                    );
                    //
                    $("#PartsBalanceAdd_AddBalanceList").find("input.inputFloat").inputFloat().end().find("input.date").each(function () {
                        var $this = $(this);
                        var opts = {};
                        if ($this.attr("format")) opts.pattern = $this.attr("format");
                        if ($this.attr("yearstart")) opts.yearstart = $this.attr("yearstart");
                        if ($this.attr("yearend")) opts.yearend = $this.attr("yearend");
                        $this.datepicker(opts);
                    });;
                    //
                    onPartsDebtSelectAddChange();
                }
               

                function onPartsDebtSelectAddDelete(aObj) {
                   

                    aObj.closest("tr").remove();

                    onPartsDebtSelectAddChange();
                }
                function onPartsDebtSelectAddChange() {
                    var $AddBalanceListTbody = $("#PartsBalanceAdd_AddBalanceList").find("tbody");
                    var AllBalance = 0;
                    $AddBalanceListTbody.find("input[name='BalanceMoney']").each(function () {
                       // var $AllBalance = $("#PartsBalanceAdd_AddBalanceList_AllBalance").val();
                        AllBalance = AllBalance + parseFloat($(this).val());
                    })
                    var $AddBalanceList_AllBalance = $("#PartsBalanceAdd_AddBalanceList_AllBalance");
                    $AddBalanceList_AllBalance.val(AllBalance); //
                    //剩余金额
                    var $ShengYuJinE = $("#PartsBalanceAdd_ShengYuJinE").val();
                    if (parseFloat($ShengYuJinE) < AllBalance) {
                        $AddBalanceList_AllBalance.css("color", "red");
                        $("#PartsBalanceAdd_AddBalanceList_AllBalance_Hit").html("冲帐金额大于剩余金额,请修改");
                        $("#PartsBalanceAddbtnSave").hide();
                    }
                    else {
                        $AddBalanceList_AllBalance.css("color", "");
                        $("#PartsBalanceAdd_AddBalanceList_AllBalance_Hit").html("");
                        $("#PartsBalanceAddbtnSave").show();
                    }
                }
            </script>
        </div>
		<div class="formBar">
			<ul>
				
				<li id="PartsBalanceAddbtnSave"><div class="buttonActive"><div class="buttonContent"><button type="submit"  >保存</button></div></div></li>
				<li>
					<div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div>
				</li>
			</ul>
		</div>
	</form>
</div>